package com.jiuqi.bi.database.metadata;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class LogicForeignKey implements Cloneable {

    private String foreignKeyName;  //外键名称

    private String subTableName;   //从表名称

    private List<String> subColumnName;  //从表列名 （与主表列 依次对应）

    private String mainTableName;  //主表名称

    private List<String> mainColumnName;  //主表列名 （与从表列 依次对应）


    private static final String  FOREIGNKEYNAME = "foreignKeyName";
    private static final String  SUBTABLENAME = "subTableName";
    private static final String  SUBCOLUMNNAME = "subColumnName";
    private static final String  MAINTABLENAME = "mainTableName";
    private static final String  MAINCOLUMNNAME = "mainColumnName";


    //region 构造器
    public LogicForeignKey() {
    }

    public LogicForeignKey(String foreignKeyName, String subTableName, List<String> subColumnName, String mainTableName, List<String> mainColumnName) {
        this.foreignKeyName = foreignKeyName;
        this.subTableName = subTableName;
        this.subColumnName = subColumnName;
        this.mainTableName = mainTableName;
        this.mainColumnName = mainColumnName;
    }

    //endregion

    //region getter(),setter()

    public String getForeignKeyName() {
        return foreignKeyName;
    }

    public void setForeignKeyName(String foreignKeyName) {
        this.foreignKeyName = foreignKeyName;
    }

    public String getSubTableName() {
        return subTableName;
    }

    public void setSubTableName(String subTableName) {
        this.subTableName = subTableName;
    }

    public List<String> getSubColumnName() {
        return subColumnName;
    }

    public void setSubColumnName(List<String> subColumnName) {
        this.subColumnName = subColumnName;
    }

    public String getMainTableName() {
        return mainTableName;
    }

    public void setMainTableName(String mainTableName) {
        this.mainTableName = mainTableName;
    }

    public List<String> getMainColumnName() {
        return mainColumnName;
    }

    public void setMainColumnName(List<String> mainColumnName) {
        this.mainColumnName = mainColumnName;
    }

    //endregion

    /**
     * 将查询结果封装到json对象中
     * @param json
     * @throws JSONException
     */
    public void toJson(JSONObject json) throws JSONException {
        json.put(FOREIGNKEYNAME, this.foreignKeyName);
        json.put(SUBTABLENAME, this.subTableName);
        json.put(SUBCOLUMNNAME, this.subColumnName);
        json.put(MAINTABLENAME, this.mainTableName);
        json.put(MAINCOLUMNNAME, this.mainColumnName);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        if(foreignKeyName != null) {
            hash += foreignKeyName.hashCode();
        }
        if(subTableName != null) {
            hash += subTableName.hashCode();
        }
        if(subColumnName != null) {
            hash += subColumnName.hashCode();
        }
        if(mainTableName != null) {
            hash += mainTableName.hashCode();
        }
        if(mainColumnName != null) {
            hash += mainColumnName.hashCode();
        }
        return hash;
    }

    @Override
    public boolean equals(Object obj) {

        /*如果两个对象的地址一致，则其是一个东西,堆中*/
        if (this == obj)
            return true;

        /*如果新传来的对象是null，因为当前对象存在则两者一定不相等*/
        if (obj == null)
            return false;

        /*如果新传来对象的类与当前对象的类不一样则两者一定不同*/
        if (this.getClass() != obj.getClass())
            return false;

        LogicForeignKey another = (LogicForeignKey)obj;
        return this.foreignKeyName.equals(another.getForeignKeyName()) ;
    }

    public LogicTable clone() {
        try {
            return (LogicTable) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e.toString());
        }
    }

    @Override
    public String toString() {
        return "LogicForeignKey{" +
                "【foreignKeyName=" + foreignKeyName + '】' +
                ",【subTableName=" + subTableName + '】' +
                ",【subColumnName=" + subColumnName.toString() + '】' +
                ",【mainTableName=" + mainTableName + '】' +
                ",【mainColumnName=" + mainColumnName.toString() + '】' +
                '}';
    }
}
